var pnum = "";
var initData = function(num){
	    var param = {
			page_num:num,
			page_size:"10"
	    }
		$.ajax({
	        url: base + "/dataCenter/businessdata/findManufacturerEquipmentPageList",
	        data: param,
	        success: function (data, textStatus, jqXHR) {
	        	var pageCount = data.pager.pageCount;
	        	var dataObj ={
	        			"list":data.list,
	         			"dataTool":{
	         				"title":{
	          			      	"manufacturer_equipment_name": "厂商设备名称"
	          			      	,"equipment_manufacturer_name": "所属厂商"
	          			      	,"equipment_price":"设备单价"
	          			      	,"equipment_power":"设备功率(kw)"
	          			      	,"equipment_model":"设备型号"
	          			      	,"create_time":"创建时间"
	          			      	,"update_time":"修改时间"
	          			      	,"operation":"操作"
	            			},
	            			"titleCheckbox":false,
							"btn":[{"event":"updateObj","name":"编辑"},
							       {"event":"updateEquipmentProperty","name":"编辑设备属性"},
							       {"event":"delObj","name":"删除"}]
						}
		  		};
	        	showPageTable('view', dataObj,num,pageCount,"initData");
	        },
	        error: function () {
	        }
	    });
}

   var searchObj = function (num) {
       if (num==undefined|| num==""||num.type=="click" ) {
           num=1;
       }
       var name=$("#searchName").val();
       var param = {
           page_num:num,
           page_size:"10",
           manufacturer_equipment_name:name
       }
       $.ajax({
           url: base + "/dataCenter/businessdata/findManufacturerEquipmentPageList",
           data: param,
           success: function (data, textStatus, jqXHR) {
               var pageCount = data.pager.pageCount;
               var dataObj ={
                   "list":data.list,
                   "dataTool":{
                       "title":{
                           "manufacturer_equipment_name": "厂商设备名称"
                           ,"equipment_manufacturer_name": "所属厂商"
                           ,"equipment_price":"设备单价"
                           ,"equipment_power":"设备功率(kw)"
                           ,"equipment_model":"设备型号"
                           ,"create_time":"创建时间"
                           ,"update_time":"修改时间"
                           ,"operation":"操作"
                       },
                       "titleCheckbox":false,
                       "btn":[{"event":"updateObj","name":"编辑"},
                           {"event":"updateEquipmentProperty","name":"编辑设备属性"},
                           {"event":"delObj","name":"删除"}]
                   }
               };
               showPageTable('view', dataObj,num,pageCount,"searchObj");
           },
           error: function () {
           }
       });
   }
   var delObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   var param = {id:obj.id}
		   $.ajax({
			   url:base+"/dataCenter/businessdata/delManfacturerEquipmentById",
			   data:param,
			   success:function(data,status){
				    if(data.statusCode == 0){
						layer.msg(data.message, {
							icon : 1
						});
				    }else{
						layer.msg(data.message, {
							icon : 5
						});
				    }
				    initData(pnum);
			   }
		   });
	   });
   }
   
   //取得表单信息
   var initFormField = function(equipment_id){
	   var contentDiv;
	   $.ajax({
		   url:base+"/datacenter/common/fileinput/"+equipment_id,
		   data:{},
		   async: false,
		   success:function(data,status){
			   contentDiv = data;
		   }
	   });
	   return contentDiv;
   }
   
   //上传设备相关资料信息
   var updateEquipmentProperty = function(obj){
	    var equipment_id = obj.id;//设备id
	    var content_div = initFormField(equipment_id);
		layer.open({
		  	title:'上传设备信息'
			,content:content_div
	    	,area: ['1400px', '900px']
			,scrollbar: false
		  	,btn: ['保存', '取消']
			,yes: function(index,layero){ //确认
			 	var data_arr = $('form').serializeArray();
			 	var data_json = {};
			 	$.each(data_arr,function(index,value){
			 		var field_name = value.name;
			 		var field_value = value.value;
			 		data_json[field_name] = field_value;
			 	});
				$.post(base+"/dataCenter/businessdata/updateEquipmentInfo",data_json,
						function (data,status){
						if(data!=""&&data!=null){
							if(data.statusCode == 0){
								layer.msg('更新成功', {icon: 1,time:2000});
							}else{
								layer.msg(data.message, {icon: 2,time:2000});
							}
						}else{
							layer.msg('更新失败!', {icon: 2,time:2000});
						}
				});
				layer.close(index);
				return false;
			}
	    });
   }
   
   
   
   
   $("body").delegate(".layui-upload-file","click",function(){
		var data_id = $(this).attr("data_id");
		upload(data_id);
	}); 
   
   var upload = function(file_name){
		var allow_format;
		if("equipment_photo"==file_name){
			allow_format = 'jpg|png|gif';
		}else{
			allow_format = 'xls|xlsx|doc|docx';
		}
		layui.upload({
			  url: base+'/datacenter/common/uploadFile?file_kind=equipmentInfo'
			  ,type:'file'
			  ,ext:allow_format
			  ,success: function(res){
				  if(res.code == '0'){
					  var file_id = res.file_id;
					  var original_file_name = res.original_file_name;
					  $("input[name='"+file_name+"_hidden']").val(file_id);
					  $("input[name='"+file_name+"_show']").val(original_file_name);
				  }else{
					  alert(res.msg);
				  }
			  }
		});
	}
   
   var layopen_updata_eip_field = function(content){
		layer.open({
		  	title:'添加设备属性'
			,content: content
	    	,area: ['800px', '700px']
		    ,scrollbar: false
		  	,btn: ['确认', '取消']
			,yes: function(index,layero){ //确认
				var data = $(layero).find("form").serializeJSON();
				if(equipment_info_verification_data(data)){
					$.post(base+"/dataCenter/businessdata/updateManufacturerEquipmentInfo",data,
					  		function (data,status){
								if(data!=""&&data!=null){
									if(data.statusCode == 0){
										layer.msg('更新成功', {icon: 1,time:2000});
									}else{
										layer.msg(data.message, {icon: 2,time:2000});
									}
								}else{
									layer.msg('更新失败!', {icon: 2,time:2000});
								}
								initData(pnum);
					  		 })
				}
			}
	});
 }

   
   //初始化设备
   var initEquipment = function(equipment_id){
	   $.post(base+"/dataCenter/businessdata/findEquipmentPageList",{},
	   		  	function(data,status){
  		    	if(data != null && data.list != null){
	      		  	 var optionstring = "";
	                 $.each(data.list, function(i,item){
	                	 if(item.id == equipment_id){
	                		optionstring += "<option selected='selected' value=\"" + item.id + "\" >" + item.equipment_name + "</option>";
	                	 }else{
	                		optionstring += "<option value=\"" + item.id + "\" >" + item.equipment_name + "</option>";
	                	 }
	                });
	                $("select[name='equipment_id']").html('<option value=""></option>' + optionstring);
	      		}else{
	      		  	$("select[name='equipment_id']").html('<option value=""></option>');
	      		}
  		    form.render(); 
	  });
   }
   
   //初始化厂商信息
   var initManfacturer = function(equipment_manufacturer_id){
	   $.post(base+"/dataCenter/businessdata/findManufacturerPageList",{},
	   		  	function(data,status){
 		    	if(data != null && data.list != null){
	      		  	 var optionstring = "";
	                 $.each(data.list, function(i,item){
	                	 if(item.id == equipment_manufacturer_id){
	                		optionstring += "<option selected='selected' value=\"" + item.id + "\" >" + item.equipment_manufacturer_name + "</option>";
	                	 }else{
	                		optionstring += "<option value=\"" + item.id + "\" >" + item.equipment_manufacturer_name + "</option>";
	                	 }
	                });
	                $("select[name='equipment_manufacturer_id']").html('<option value=""></option>' + optionstring);
	      		}else{
	      		  	$("select[name='equipment_manufacturer_id']").html('<option value=""></option>');
	      		}
 		    form.render(); 
	  });
   }
   
   
   var addObj = function(){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "equipment_type_id":"设备类型",
					   "equipment_cid_type_id":"设备子类型",
					   "equipment_id":"所属设备",
					   "equipment_manufacturer_id":"所属厂商",
					   "manufacturer_equipment_name":"设备名称",
					   "equipment_price":"设备单价",
					   "equipment_power":"设备功率",
					   "equipment_model":"设备型号"
				   },"text":{
					   "equipment_type_id":"",
					   "equipment_cid_type_id":"",
					   "equipment_id":"",
					   "equipment_manufacturer_id":"",
					   "manufacturer_equipment_name":"",
					   "equipment_price":"",
					   "equipment_power":"",
					   "equipment_model":""
				   },"input":{
					   "equipment_type_id":"select",
					   "equipment_cid_type_id":"new_select",
					   "equipment_id":"search_select",
					   "equipment_manufacturer_id":"search_select",
					   "manufacturer_equipment_name":"text",
					   "equipment_price":"text",
					   "equipment_power":"text",
					   "equipment_model":"text"
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
	   initEquipemntType();
	   chanageEquipmentType();
	   chanageEquipmentCidType();
	   initSwitch();
	   initManfacturer();
   }
   
   var initSwitch = function(flag){
	   var html = '';
	   if(flag=="true" || flag=="1"){
		   html += '<input checked="" name="is_not_basics" lay-skin="switch" lay-filter="switch" lay-text="有|无" type="checkbox" > ';
	   }else{
		   html += '<input name="is_not_basics" lay-skin="switch" lay-filter="switch" lay-text="有|无" type="checkbox" > ';
	   }
	   $("#switch").html(html);
	   form.render(); 
   }
   
   
   //初始化设备类型
   var initEquipemntType = function(){
	   $.post(base+"/dataCenter/businessdata/findEquipmentType",{},
	   		  	function(data,status){
    		    	if(data != null && data.list != null){
  	      		  	 var optionstring = "";
  	                 $.each(data.list, function(i,item){
  	                	optionstring += "<option value=\"" + item.id + "\" >" + item.equipment_type_name + "</option>";
  	                });
  	                $("select[name='equipment_type_id']").html('<option value=""></option>' + optionstring);
  	      		}else{
  	      		  	$("select[name='equipment_type_id']").html('<option value=""></option>');
  	      		}
    		    form.render(); 
	  });
   }
   
   //监听设备类型下拉
   var chanageEquipmentType = function(){
 		form.on("select(select)",function(value){
  			var equ_type_id = value.value;
  			initEquipmentCidType(equ_type_id);
  		});
  }
   
   //初始化设备子类型
   var initEquipmentCidType = function(equipment_type_id){
	   $.post(base+"/dataCenter/businessdata/findEquipmentCidTypeList",{equipment_type_id:equipment_type_id},
	   		  	function(data,status){
   		    	if(data != null && data.list != null){
 	      		  	 var optionstring = "";
 	                 $.each(data.list, function(i,item){
 	                	optionstring += "<option value=\"" + item.id + "\" >" + item.equipment_children_type_name + "</option>";
 	                });
 	                $("select[name='equipment_cid_type_id']").html('<option value=""></option>' + optionstring);
 	      		}else{
 	      		  	$("select[name='equipment_cid_type_id']").html('<option value=""></option>');
 	      		}
   		    form.render(); 
	  });
   }
   
   //监听子类型下拉
   var chanageEquipmentCidType = function(){
 		form.on("select(new_select)",function(value){
   			var equ_cid_type_id = value.value;
   			initEquipmentInfo(equ_cid_type_id);
   		});
   }
   
   //初始化设备
   var initEquipmentInfo = function(equipment_cid_type_id){
	   $.post(base+"/dataCenter/businessdata/findEquipmentPageList",{equipment_children_type_id:equipment_cid_type_id},
	   		  	function(data,status){
  		    	if(data != null && data.list != null){
	      		  	 var optionstring = "";
	                 $.each(data.list, function(i,item){
	                	optionstring += "<option value=\"" + item.id + "\" >" + item.equipment_name + "</option>";
	                });
	                $("select[name='equipment_id']").html('<option value=""></option>' + optionstring);
	      		}else{
	      		  	$("select[name='equipment_id']").html('<option value=""></option>');
	      		}
  		    form.render(); 
	  });
   }
   
   var updateObj = function(obj){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "id":"唯一标识",
					   "equipment_manufacturer_id":"所属厂商",
					   "manufacturer_equipment_name":"设备名称",
					   "equipment_price":"设备单价",
					   "equipment_power":"设备功率",
					   "equipment_model":"设备型号"
				   },"text":{
					   "id":obj.id,
					   "equipment_manufacturer_id":"",
					   "manufacturer_equipment_name":obj.manufacturer_equipment_name,
					   "equipment_price":obj.equipment_price,
					   "equipment_power":obj.equipment_power,
					   "equipment_model":obj.equipment_model
				   },"input":{
					   "id":"hidden",
					   "equipment_manufacturer_id":"select",
					   "manufacturer_equipment_name":"text",
					   "equipment_price":"text",
					   "equipment_power":"text",
					   "equipment_model":"text"
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
	   initManfacturer(obj.equipment_manufacturer_id);
	   initSwitch(obj.is_not_basics);
   }
   
   var equipment_info_verification_data = function(data){
	   if(data.equipment_basics == ""){
		   var obj = $("textarea[name='equipment_basics']");
		   obj.focus();
		   layer.tips('请填写此字段!', obj);
		   return false;
	   }
	   if(data.equipment_photo_hidden == ""){
		   var obj = $("input[name='equipment_photo_show']");
		   obj.focus();
		   layer.tips('请上传设备照片!', obj);
		   return false;
	   }
	   if(data.equipment_data_form_hidden == ""){
		   var obj = $("input[name='equipment_data_form_show']");
		   obj.focus();
		   layer.tips('请上传设备资料提供单!', obj);
		   return false;
	   }
	   if(data.equipment_demand_book_hidden == ""){
		   var obj = $("input[name='equipment_demand_book_show']");
		   obj.focus();
		   layer.tips('请上传用户需求说明书 !', obj);
		   return false;
	   }
	   return true;
   }
   
   
   var layopen = function(content){
		layer.open({
		  	title:'更新厂商设备'
			,content: content
	    	,area: ['800px', '500px']
		    ,scrollbar: false
		  	,btn: ['确认', '取消']
			,yes: function(index,layero){ //确认
				var data = $(layero).find("form").serializeJSON();
				if(data.equipment_id == "" || data.equipment_manufacturer_id == ""){
					alert("缺少参数!");
					return false;
				}
				if(data.manufacturer_equipment_name == ""){
					var obj = $("input[name='manufacturer_equipment_name']");
					obj.focus();
					layer.tips('请填写此字段!', obj);
					return false;
				}
				if(data.equipment_price == ""){
					var obj = $("input[name='equipment_price']");
					obj.focus();
					layer.tips('请填写此字段!', obj);
					return false;
				}
				if(data.equipment_power == ""){
					var obj = $("input[name='equipment_power']");
					obj.focus();
					layer.tips('请填写此字段!', obj);
					return false;
				}
				if(data.equipment_model == ""){
					var obj = $("input[name='equipment_model']");
					obj.focus();
					layer.tips('请填写此字段!', obj);
					return false;
				}
				var equipment_manufacturer_name = $("select[name='equipment_manufacturer_id']").find("option:selected").text()
				$.post(base+"/dataCenter/businessdata/updateManufacturerEquipment",{
					id:data.id==null?"":data.id,
					manufacturer_equipment_name:$.trim(data.manufacturer_equipment_name),
					equipment_id:$.trim(data.equipment_id),
					equipment_price:$.trim(data.equipment_price),
					equipment_manufacturer_id:$.trim(data.equipment_manufacturer_id),
					equipment_manufacturer_name:$.trim(equipment_manufacturer_name),
					equipment_power:$.trim(data.equipment_power),
					equipment_model:$.trim(data.equipment_model)
				},
		  		function (data,status){
					if(data!=""&&data!=null){
						if(data.statusCode == 0){
							layer.msg('更新成功', {icon: 1,time:2000});
						}else{
							layer.msg(data.message, {icon: 2,time:2000});
						}
					}else{
						layer.msg('更新失败!', {icon: 2,time:2000});
					}
					initData(pnum);
		  		 })
			}
	});
  }
   
   $(document).ready(function(){
	   initData(1);
	   $("#addData").click(addObj);
       $("#searchInfo").click(searchObj);
   });
   
   
   
   
   
   
   
   
   